﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    public class DirectUIContainer : DirectUIControl
    {
        private Dictionary<DirectUIControl, Rectangle> collectionRectangle = new Dictionary<DirectUIControl, Rectangle>();

        public DirectUIContainer()
        {
            this.Controls = new DirectUIControlCollection(this);
        }

        /// <summary>
        /// 获取包含在控件内的组件的集合。
        /// </summary>
        public DirectUIControlCollection Controls { get; }

        public override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            foreach (var control in this.Controls)
            {
                if (control.Dock != DockStyle.None | control.Anchor != (AnchorStyles.Left | AnchorStyles.Top))
                    control.refreshLayout();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            collectionRectangle.Clear();

            base.OnPaint(e);
            int verticalScroll = 0; //垂直滚动条的位置。
            int horizontalScroll = 0;//水平滚动条的位置。
            float preLeft = 0; //记录上一个组件的X坐标
            float preTop = 0; //记录上一个组件的Y坐标。
            float offsetX = e.Graphics.Transform.OffsetX;
            float offsetY = e.Graphics.Transform.OffsetY;
            foreach (var control in this.Controls)
            {
                Rectangle clipRect = new Rectangle(control.Location, control.Size);
                if (clipRect.Bottom > verticalScroll && clipRect.Top < verticalScroll + this.Height && clipRect.Right > horizontalScroll && clipRect.Left < horizontalScroll + this.Width)
                {
                    e.Graphics.TranslateTransform(control.Left - preLeft, control.Top - preTop);
                    preLeft = control.Left;
                    preTop = control.Top;
                    collectionRectangle[control] = clipRect;
                    clipRect = new Rectangle(0, 0, Math.Min(control.Width, this.Width - control.Left), Math.Min(control.Height, this.Height - control.Top));
                    e.Graphics.SetClip(clipRect);
                    PaintEventArgs args = new PaintEventArgs(e.Graphics, clipRect);
                    control.Draw(args);
                    e.Graphics.FillRectangle(Brushes.Yellow, clipRect);
                }
            }
            e.Graphics.TranslateTransform(offsetX - e.Graphics.Transform.OffsetX, offsetY - e.Graphics.Transform.OffsetY);
        }



        public override void OnMouseClick(MouseEventArgs e)
        {
            DirectUIControl control = collectionRectangle.Reverse().FirstOrDefault(x => x.Value.Contains(e.Location)).Key;
            if (control is not null)
            {
                MouseEventArgs args = new MouseEventArgs(e.Button, e.Clicks, e.X - control.Left, e.Y - control.Top, e.Delta);
                control.OnMouseClick(args);
            }
            else
            {
                base.OnMouseClick(e);
            }
        }

        /// <summary>
        /// 计算布局信息。
        /// </summary>
        public void CalculatingLayoutInformation()
        {

        }
    }
}
